﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using SongSharp.Presentation.ViewModel.Data;
using SongSharp.Domain.Tagging;

namespace SongSharp.Presentation.ViewModel
{
    internal class ID3TagCrawler : BackgroundWorker
    {
        private PrimeViewModel ViewModel { get; set; }

        public ID3TagCrawler(PrimeViewModel viewModel)
        {
            ViewModel = viewModel;

            WorkerSupportsCancellation = true;
            DoWork += new DoWorkEventHandler(ID3TagCrawler_DoWork);
            RunWorkerCompleted += new RunWorkerCompletedEventHandler(ID3TagCrawler_RunWorkerCompleted);
        }

        void ID3TagCrawler_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            ViewModel.UpdateTrackList();
        }

        void ID3TagCrawler_DoWork(object sender, DoWorkEventArgs e)
        {
            ObservableCollection<TrackViewData> list = (ObservableCollection<TrackViewData>)e.Argument;
            foreach (TrackViewData t in list)
            {
                TagInfo info = TagReader.Read(t.TrackObject);
                t.Title = info.Title;
                t.Artist = info.Artist;
                t.Year = info.Year;
                t.Album = info.Album;
                if (info.Length.TotalHours >= 1.0)
                    t.Length = string.Format("{0:00}", info.Length.Hours) + ":" + string.Format("{0:00}", info.Length.Minutes) + ":" +
                         string.Format("{0:00}", info.Length.Seconds);
                else
                    t.Length = string.Format("{0:00}", info.Length.Minutes) + ":" +
                         string.Format("{0:00}", info.Length.Seconds);
                t.Dirty = false;
                t.VVV = string.Format("{0:F2}", (t.TrackObject.Statistic.VaryVogueValue * 10));
            }
        }


    }
}
